package cn.kgc.vue.handlers;

import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.exceptions.ExamTimeException;
import cn.kgc.vue.exceptions.StudentNoException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 课工场
 * @date 2024/4/10
 * @description 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult lockedAccountExceptionHandler(LockedAccountException e) {
        e.printStackTrace();
        return ResponseResult.error().message("账号被禁用,请联系管理员");
    }


    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult unknownAccountExceptionHandler(UnknownAccountException e) {
        e.printStackTrace();
        return ResponseResult.error().message("用户名不存在");
    }


    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult incorrectCredentialsExceptionHandler(IncorrectCredentialsException e) {
        e.printStackTrace();
        return ResponseResult.error().message("密码错误");
    }


    @ExceptionHandler(AuthorizationException.class)
    public ResponseResult authorizationExceptionHandler(Exception e) {
        e.printStackTrace();
        return ResponseResult.error().message("没有权限，请联系管理员");
    }


    @ExceptionHandler(StudentNoException.class)
    public ResponseResult studentNoExceptionHandler(StudentNoException e) {
        e.printStackTrace();
        String info = e.getInfo();
        return ResponseResult.error().message(info);
    }


    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseResult allExceptionHandler(DuplicateKeyException e) {
        e.printStackTrace();
        return ResponseResult.error().message("添加数据已存在,请检查");
    }


    // ExamTimeException
    @ExceptionHandler(ExamTimeException.class)
    public ResponseResult examTimeExceptionHandler(ExamTimeException e) {
        e.printStackTrace();
        return ResponseResult.error().message(e.getInfo());
    }
    @ExceptionHandler(Exception.class)
    public ResponseResult allExceptionHandler(Exception e) {
        e.printStackTrace();
        return ResponseResult.error().message("服务器繁忙,请稍后再试");
    }
}
